//
//  OptionTableView.m
//  BOSS-SOOC
//
//  Created by apple on 16/8/24.
//  Copyright © 2016年 Gcx. All rights reserved.
//

#import "OptionTableView.h"
#import "OptionCell.h"
#import "UITableView+SCKit.h"
#import "SCTableView.h"
#import "UIView+WHC_AutoLayout.h"

@interface OptionTableView()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray<OptionModel *> *orders;

@end

@implementation OptionTableView


- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{

    self = [super initWithFrame:frame style:style];
    
    if (self) {
        
        self.dataSource = self;
        self.delegate = self;
        
        [self registerClass:[OptionCell class] forCellReuseIdentifier:@"OptionCell"];
        
        [self setupRefresh];

        [self emptyWithTitle:@"暂无订单" image:[UIImage imageNamed:@"空白页-搜索无数据图片"]];
        
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(isReload) name:@"optionListRefresh" object:nil];

    }
    return self;
}

- (void)isReload{

    [self isReloadData:YES];
}

/**
 *  数据加载
 *
 *  @param reload 是否重置数据
 */
- (void)isReloadData:(BOOL)reload{

    NSInteger pageIndex = 1;
    
    if (reload) {
        
    } else {
        pageIndex = [self sc_currentPageNumWithTotalCount:self.orders.count];
    }

    NSMutableDictionary *parms = [[NSMutableDictionary alloc]init];
    [parms setObject:@"815bffb68e2ea44e38cf16a98a5a29a4" forKey:@"oauth_token"];
    [parms setObject:@"8e84bc64f03d73b5c75406ef1d7349f7" forKey:@"oauth_token_secret"];
    [parms setObject:@"1" forKey:@"my"];
    [parms setObject:@10 forKey:@"num"];
    [parms setObject:[NSString stringWithFormat:@"%ld",(long)pageIndex] forKey:@"page"];
    
    SCHttpRequest *request = [[SCHttpRequest alloc]initWithApi:API_myFeedback
                                                        params:parms];
    [self doPOSTConnect:request start:^(SCHttpRequest * _Nonnull req) {
        
    } success:^(SCHttpRequest * _Nonnull req, SCHttpResponse * _Nonnull resp) {
        
        NSArray<OptionModel*> *newArr = [OptionModel mj_objectArrayWithKeyValuesArray:resp.array];
        
        if (reload) {
            [self.orders removeAllObjects];
        }
        
        [self.orders addObjectsFromArray:newArr];
        
        [self reloadData];
        
    } failure:^(SCHttpRequest * _Nonnull req, NSError * _Nonnull error) {
        
    } finish:^(SCHttpRequest * _Nonnull req, BOOL success) {
        
//        self.showEmpty = YES;
        
//        __block __weak OptionTableView *table = self;

        [self.mj_header endRefreshing];
        [self.mj_footer endRefreshing];
        
    }];
}

/**
 *  在没有内容的情况下获取内容
 */
- (void)loadDataOfNone{
    if (self.orders.count == 0) {
//        [self.mj_header beginRefreshing];
        [self isReloadData:YES];
    }
}

/**
 *  重置内部数据
 */
- (void)reset{
    
    [self.orders removeAllObjects];
    
    [self reloadData];
}


/**
 *  刷新加载
 */
- (void)setupRefresh{
    
//    __block __weak OptionTableView *table = self;
    
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        [self.mj_header beginRefreshing];
        
        [self isReloadData:YES];
        
    }];
    header.stateLabel.textColor = COLOR_TITLE_999999;
    header.lastUpdatedTimeLabel.textColor = COLOR_TITLE_999999;
    self.mj_header = header;
    
    
    MJRefreshAutoNormalFooter * foot = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        [self.mj_footer beginRefreshing];
        
        [self isReloadData:NO];
    }];
    foot.stateLabel.textColor = COLOR_TITLE_999999;
    self.mj_footer = foot;
    
}

#pragma mark -- DataSource

- (NSInteger)numberOfRowsInSection:(NSInteger)section{
    
    if (self.orders.count>0) {
        return 1;
    }else{
        return 0;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.orders.count;

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [OptionCell whc_CellHeightForIndexPath:indexPath tableView:tableView]+ScaleSize(60);
<<<<<<< HEAD

}

=======

}
>>>>>>> 1dcd124a62c1f4d5dff9fbf868fe64c33aa8265f

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0.001f;
    }
    
    return 10.0f;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    OptionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OptionCell"];
    
    [cell initWithModel:self.orders[indexPath.row]];
    
    return cell;
}

#pragma mark - Delegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    OptionModel *model = self.orders[indexPath.row];
    
    if (self.sc_delegate && [self.sc_delegate respondsToSelector:@selector(tableView:didSelectModel:)]) {
        [self.sc_delegate tableView:self didSelectModel:model];
    }
}

- (NSMutableArray<OptionModel*>*)orders{

    if (!_orders) {
        _orders = [[NSMutableArray alloc]init];
    }
    return _orders;
}

@end
